import pymysql
import requests

url="http://192.168.124.119/user.php"
#登录
login={"username":"xxy","password":"xxy123","act":"act_login","submit":""}
r=requests.post(url,data=login)
# print(r.raw)
print(r.text)
# 获取cookies
mcook=r.cookies
# print(mcook)
#设置参数
ldata={"act":"message_list","order_id":"0"}
#将登陆后的cookies存储到我们的header或者传给cookies
#请求我的留言
r2=requests.request("get",url,params=ldata,cookies=mcook)
if "我的留言" in r2.text:
    print("请求成功")
#新增我的留言
msgdata={"msg_type":"0","msg_title":"python自动化表头","msg_content":"python自动化留言内容","act":"act_add_message"}
f=open(r"C:\Users\立哲\Pictures\Saved Pictures\01.jpg","rb")
msgfile={"message_img":("01.jpg",f,"image/jpg")}
rr=requests.request("post",url,data=msgdata,files=msgfile,cookies=mcook)
print("成功" in rr.text)






#删除留言

db=pymysql.connect(user='root',password='123456',host='192.168.124.119',port=3306,database='ecshop')
#编写sql语句
sql1="SELECT msg_id FROM ecs_feedback where user_name='xxx'"
#执行sql语句
#申请执行者
zx=db.cursor()
i=zx.execute(sql1)
t=zx.fetchall()
print(i)
print(t)

#批量删除留言
# act=del_msg&id=2&order_id=0

for i in t:
    print(i)
    pdata = {"act": "del_msg", "id": i[0], "order": 0}

    requests.get(url,params=pdata,cookies=mcook)

#验证是否删除成功
i=zx.execute(sql1)
# print(i == 0)
if i == 0:
    print("删除成功")

zx.close()
db.close()